package com.hz.web.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hz.web.entity.Sheet;
import com.hz.web.service.SheetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by kaddddd on 2018/5/11 0011.
 */
@Controller
public class SheetController {
    @Autowired
    SheetService sheetService;

    @RequestMapping("/sheetList")
    public @ResponseBody String getSheetList(ModelMap model){
        int status = 1;
        List<Sheet> list = sheetService.getSheetList(status);
        String sheetList = JSON.toJSONString(list);
        return sheetList;
    }

    @RequestMapping(value="/sheetDetail/{id}", method= RequestMethod.GET)
    public @ResponseBody Sheet sheetDetail(@PathVariable int id, ModelMap model){
        Sheet sheet = sheetService.sheetDetail(id);
        return sheet;
    }

    private String getPrincipal(){
        String userName = null;
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        if (principal instanceof UserDetails) {
            userName = ((UserDetails)principal).getUsername();
        } else {
            userName = principal.toString();
        }
        return userName;
    }
}
